package web.login;

import domain.User;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.PasswordResetService;
import service.facade.user.UserService;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class ResetPasswordPanel extends Panel {

    @SpringBean
    private PasswordResetService passwordService;
    @SpringBean
    private UserService userService;
    private Form mainForm;
    private ResetModel rm = new ResetModel();
    private final String key;

    public ResetPasswordPanel(String id, String key) {
        super(id);
        this.key = key;
        mainForm = new Form("mainForm");
        showTextFields();
        showButtons();
        add(mainForm);
    }

    private void showTextFields() {
        mainForm.add(new TextField("loginResetField", new PropertyModel(rm, "login")));
        mainForm.add(new TextField("emailResetField", new PropertyModel(rm, "email")));
    }

    private void showButtons() {
        mainForm.add(new Button("sendButton") {

            @Override
            public void onSubmit() {
                User u = null;
                try {
                    u = userService.findUserByLogin(rm.getLogin());
                } catch (Exception e) {
                    //ok
                }
                if (u == null || !u.getCompanyEmail().equalsIgnoreCase(rm.getEmail())) {
                    error(getLocalizer().getString("password.fail.login", this));
                } else {
                    try {
                        if (key != null) {
                            passwordService.generateNewPassword(rm.getLogin(), rm.getEmail(), key);
                            info(getLocalizer().getString("password.success.change", this));

                        } else {
                            passwordService.requestPasswordReset(rm.getLogin(), rm.getEmail());
                            info(getLocalizer().getString("password.success.reset", this));
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(ResetPasswordPanel.class.getName()).log(Level.SEVERE, null, ex);
                        error(getLocalizer().getString("password.fail", this));
                    }
                }
            }
        });
    }

    private class ResetModel implements Serializable {

        private static final long serialVersionUID = 1L;
        private String login;
        private String email;

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getLogin() {
            return login;
        }

        public void setLogin(String login) {
            this.login = login;
        }
    }
}
